package com.hygj.util;


import org.springframework.web.util.HtmlUtils;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtil {
    // 定义script的正则表达式
    private static final String REG_EX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>";
    // 定义style的正则表达式
    private static final String REG_EX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>";
    // 定义HTML标签的正则表达式
    private static final String REG_EX_HTML = "<[^>]+>";
    // 定义空格回车换行符¬
    private static final String REG_EX_SPACE = "\\s*|\t|\r|\n";

    private interface HtmlEscape {
        /**
         * 解析数据
         *
         * @param str
         * @return
         */
        String escape(String str);
    }

    /**
     * 数据解析方法
     *
     * @param t
     * @param f
     * @param htmlEscape
     * @param <T>
     */
    private static <T> void htmlUnescape(T t, String[] f, HtmlEscape htmlEscape) {
        Class clazz = t.getClass();
        //
        try {
            List<String> list = new ArrayList<>();
            if (f != null) {
                list.addAll(Arrays.asList(f));
            }
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                if (list.contains(field.getName())) {
                    continue;
                }
                field.setAccessible(true);
                if (field.getType() == String.class) {
                    Object o = field.get(t);
                    if (o != null) {
                        field.set(t, htmlEscape.escape(String.valueOf(o)));
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 转义
     * 利用反射把类中的字符串转义
     *
     * @param t
     * @param f 过滤字段
     * @return
     */
    public static <T> void htmlEscape(T t, String[] f) {
        htmlUnescape(t, f, HtmlUtils::htmlEscape);
    }

    /**
     * 读取适合用  extends
     * 插入适合用 super
     *
     * @param t
     * @param <T>
     */
    public static <T> void htmlEscape(T t) {
        htmlEscape(t, null);
    }

    /**
     * 将类中字符串反转义
     *
     * @param t
     * @param f 过滤字段
     * @return
     */
    public static <T> void htmlUnescape(T t, String[] f) {
        htmlUnescape(t, f, HtmlUtils::htmlUnescape);
    }

    public static <T> void htmlUnescape(T t) {
        htmlUnescape(t, null);
    }


    /**
     * 删除Html标签
     *
     * @param htmlStr
     * @return
     */
    public static String delHTMLTag(String htmlStr) {
        Pattern pScript = Pattern.compile(REG_EX_SCRIPT, Pattern.CASE_INSENSITIVE);
        Matcher mScript = pScript.matcher(htmlStr);
        // 过滤script标签
        htmlStr = mScript.replaceAll("");

        Pattern pStyle = Pattern.compile(REG_EX_STYLE, Pattern.CASE_INSENSITIVE);
        Matcher mStyle = pStyle.matcher(htmlStr);
        // 过滤style标签
        htmlStr = mStyle.replaceAll("");

        Pattern pHtml = Pattern.compile(REG_EX_HTML, Pattern.CASE_INSENSITIVE);
        Matcher mHtml = pHtml.matcher(htmlStr);
        // 过滤html标签
        htmlStr = mHtml.replaceAll("");

        Pattern pSpace = Pattern.compile(REG_EX_SPACE, Pattern.CASE_INSENSITIVE);
        Matcher mSpace = pSpace.matcher(htmlStr);
        // 过滤空格回车标签
        htmlStr = mSpace.replaceAll("");
        // 返回文本字符串
        return htmlStr.trim();
    }

    /**
     * 吧一段字符串分解成 map对象
     *
     * @return
     * @throws IOException
     */
    public static Map<String, String> selectAsMap(String fileStr)
            throws IOException {
        Map<String, String> selects = new HashMap<>();

        File file = new File(fileStr);
        InputStreamReader in = new InputStreamReader(new FileInputStream(file));
        BufferedReader bufferedReader = new BufferedReader(in);
        StringBuilder result = new StringBuilder();
        String a = "";
        while ((a = bufferedReader.readLine()) != null) {
            result.append(a);
        }
        in.close();
        String p2 = "\"[\\s\\S]*?\">[\\s\\S]*?<";
        Pattern pattern = Pattern.compile(p2);
        Matcher matcher = pattern.matcher(result.toString());
        while (matcher.find()) {
            String group = matcher.group();
            if (!group.contains("http")) {
                continue;
            }
            group = group.trim();
            // System.out.println(group);
            String g1 = group.substring(0, group.indexOf(">"));
            g1 = g1.replace("\"", "").trim();
            String g2 = group.substring(group.indexOf(">") + 1);
            g2 = g2.replace("<", "").trim();
            selects.put(g1, g2);
        }
        return selects;
    }

    public static void main(String[] args) throws IOException {
        String str = "<div style='text-align:center;'>&nbsp;整治“四风”&nbsp;&nbsp;&nbsp;清弊除垢<br/><span style='font-size:14px;'>&nbsp;</span><span style='font-size:18px;'>公司召开党的群众路线教育实践活动动员大会</span><br/></div>";
        System.out.println(HtmlUtils.htmlEscape(str));
        System.out.println(HtmlUtils.htmlEscapeDecimal(str));
        System.out.println(HtmlUtils.htmlEscapeHex(str));
        System.out.println(HtmlUtils.htmlUnescape(str));
        // HtmlUtil.selectAsMap("/Users/suzhen/Desktop/各市招商局");


    }
}
